<?php

namespace frontend\modules\member\controllers;

use common\models\Member;
use yii\rest\Controller;
use yii\web\Response;

/**
 * Default controller for the `member` module
 */
class IndexController extends Controller
{

    public function behaviors()
    {
        $behaviors                                 = parent::behaviors();
        $behaviors['contentNegotiator']['formats'] = ['application/json' => Response::FORMAT_JSON];

        return $behaviors;
    }

    protected function verbs()
    {
        return [
            'sign-up' => ['post'],
            'login'   => ['post'],
        ];
    }

    /**
     * @return bool
     * @throws \yii\base\Exception
     */
    public function actionSignUp()
    {
        $member                  = new Member();
        $member->member_password = \Yii::$app->request->body('password');
        $member->member_email    = \Yii::$app->request->body('email');
        $member->member_phone    = \Yii::$app->request->body('phone');

        return $member->signUp();
    }

    /**
     * Logs in a user.
     *
     * @return mixed
     * @throws \Exception
     */
    public function actionLogin()
    {
        $phone    = \Yii::$app->request->body('phone');
        $password = \Yii::$app->request->body('password');

        return Member::login($phone, $password);
    }

}
